package cn.xutongxue.gtms.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.xutongxue.gtms.entity.File;
import cn.xutongxue.gtms.entity.vo.ManagerVo;
import cn.xutongxue.gtms.entity.vo.TeacherGuideVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.xutongxue.gtms.service.FileService;
import cn.xutongxue.gtms.mapper.FileMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【sys_file(文件附件表)】的数据库操作Service实现
 * @createDate 2023-11-27 14:00:14
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File>
        implements FileService {

    @Resource
    private UserServiceImpl userService;

    @Override
    public List<ManagerVo> getGuideList() {
        Long studentId = StpUtil.getLoginIdAsLong();
        List<File> list = list(new LambdaQueryWrapper<File>().eq(File::getUploader, studentId)
                .ne(File::getType, "指导材料")
                .ne(File::getType, "头像"));
        if (CollectionUtil.isNotEmpty(list)) {
            return BeanUtil.copyToList(list, ManagerVo.class);
        }
        return new ArrayList<>();
    }

    @Override
    public List<File> getFileList() {
        return list(new LambdaQueryWrapper<File>().eq(File::getType, "指导材料"));
    }

    @Override
    public Page<TeacherGuideVo> getGuideInfoList(Integer current, Integer size) {

        Long teacherId = StpUtil.getLoginIdAsLong();
        Page<TeacherGuideVo> teacherGuideVos = userService.getStudentListByTeacherId(teacherId, current, size);
        List<TeacherGuideVo> records = teacherGuideVos.getRecords();
        // 查询出当前学生上传的所有材料
        for (TeacherGuideVo record : records) {
            TeacherGuideVo teacherGuideVo = new TeacherGuideVo();
            BeanUtil.copyProperties(record, teacherGuideVo);
            List<File> files = list(new LambdaQueryWrapper<File>().eq(File::getUploader, record.getId())
                    .ne(File::getType, "指导材料")
                    .ne(File::getType, "头像"));
            record.setFiles(files);
        }
        return teacherGuideVos;
    }

}




